package com.yss.sofa.report.engine.util;

public class StringUtil {
    
    public static String rmbToBigString(double value) {
        char[] hunit = new char[]{'拾', '佰', '仟'};
        char[] vunit = new char[]{'万', '亿'};
        char[] digit = new char[]{'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};
        long midVal = (long) (value * 100.0D);
        if (midVal < 10L) {
            return digit[Integer.parseInt(midVal + "")] + "分";
        } else {
            String valStr = String.valueOf(midVal);
            int n = valStr.length() - 2;
            String head = valStr.substring(0, n);
            String rail = valStr.substring(n);
            String prefix = "";
            String suffix = "";
            if (rail.equals("00")) {
                suffix = "整";
            } else {
                suffix = digit[rail.charAt(0) - 48] + "角" + digit[rail.charAt(1) - 48] + "分";
            }

            char[] chDig = head.toCharArray();
            char zero = '0';
            byte zeroSerNum = 0;

            for (int i = 0; i < chDig.length; ++i) {
                int idx = (chDig.length - i - 1) % 4;
                int vidx = (chDig.length - i - 1) / 4;
                if (chDig[i] == '0') {
                    ++zeroSerNum;
                    if (zero == '0') {
                        zero = digit[0];
                    } else if (idx == 0 && vidx > 0 && zeroSerNum < 4) {
                        prefix = prefix + vunit[vidx - 1];
                        zero = '0';
                    }
                } else {
                    zeroSerNum = 0;
                    if (zero != '0') {
                        prefix = prefix + zero;
                        zero = '0';
                    }

                    prefix = prefix + digit[chDig[i] - 48];
                    if (idx > 0) {
                        prefix = prefix + hunit[idx - 1];
                    }

                    if (idx == 0 && vidx > 0) {
                        prefix = prefix + vunit[vidx - 1];
                    }
                }
            }

            if (prefix.length() > 0) {
                prefix = prefix + '圆';
            }

            return prefix + suffix;
        }
    }

    public static void main(String[] args) {
        System.out.println(rmbToBigString(10D));
    }
}
